/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * MeetTaskMasterService
 * 任务信息主表
 * <p>
 * 修改纪录
 * 2020-08-31 版本：1.0 shenyouyi 创建。
 *
 * @version 版本：1.0
 * @author 作者：shenyouyi
 * 创建日期：2020-08-31
 */
package com.topscomm.meet.service.impl;

import com.alibaba.fastjson.JSON;
import com.topscomm.base.service.IBaseCboUserExtendService;
import com.topscomm.base.service.IBaseTeamUserService;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.BusinessException;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboOrgEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboDeptService;
import com.topscomm.cbo.service.ICboOrgService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.meet.pojo.MeetTaskMasterEntity;
import com.topscomm.meet.service.IMeetTaskMasterService;
import com.topscomm.meet.service.IMeetTaskService;
import com.topscomm.meet.service.ISmsWorkTasksCloseService;
import com.topscomm.meet.service.impl.auto.MeetTaskMasterServiceAuto;
import com.topscomm.meet.vo.OAResult;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service(value = "meetTaskMasterService")
@Scope(value = "singleton")
public class MeetTaskMasterService extends MeetTaskMasterServiceAuto implements IMeetTaskMasterService {

    @Autowired
    private IBaseCboUserExtendService baseCboUserExtendService;
    @Autowired
    private IBaseTeamUserService baseTeamUserService;
    @Autowired
    private ISmsWorkTasksCloseService smsWorkTasksCloseService;
    @Autowired
    private ICboUserService cboUserService;
    @Autowired
    private ICboDeptService cboDeptService;
    @Autowired
    private ICboOrgService cboOrgService;
    @Autowired
    private IMeetTaskService meetTaskService;

    /**
     * @author: shenyouyi
     * @date: 2020-08-31
     * @param: paraMap
     * @return
     * @description: 查询选择的角色、部门、团队、人员是否包含用户
     * @modify:
     */
    @Override
    public boolean searchHaveUserByCondition(Map<String, Object> paraMap) {
        // 判断是否选择了办理人
        String handleusercodes = (String) paraMap.get("handleusercodes");
        if(!StringUtil.isEmpty(handleusercodes)){
            return true;
        }
        // 根据角色查询是否有人员
        String roleids = ConvertUtil.convertToString(paraMap.get("handleroleids"));
        boolean haveUser = baseCboUserExtendService.searchHaveUserByRoleIds(roleids);
        if (haveUser) {
            return true;
        }
        // 根据部门查询是否有人员
        String deptcodes = ConvertUtil.convertToString(paraMap.get("handledeptcodes"));
        haveUser = baseCboUserExtendService.searchHaveUsersByDeptCodes(deptcodes);
        if (haveUser) {
            return true;
        }
        // 根据团队查询是否有人员
        String teamids = ConvertUtil.convertToString(paraMap.get("handleteamids"));
        haveUser = baseTeamUserService.searchHaveUsersByTeams(teamids);
        return haveUser;
    }

    /**
     * @author: shenyouyi
     * @date: 2020-08-31
     * @return
     * @description: 从OA系统获取编码值
     * @modify:
     */
    @Override
    public Map<String, Object> searchCodeMapFromOA() {
        String oaResultJson = smsWorkTasksCloseService.createItemListFromMeet();
        OAResult<String> oaResult = JSON.parseObject(oaResultJson, OAResult.class);
        if (!oaResult.getSuccess()) {
            throw new BusinessException("查询OA系统任务列表失败，请稍后重试。");
        }
        String jsonData = oaResult.getData();
        Map<String, Object> codeMap = JSON.parseObject(jsonData, Map.class);
        return codeMap;
    }

    /**
     * @author: shenyouyi
     * @date: 2020-09-04 
     * @param: paraMap
     * @return 
     * @description: 根据条件查询用户数量
     * @modify:
     */
    @Override
    public int searchCountUserByCondition(Map<String, Object> paraMap) {
        List<Map<String, Object>> userMapList = meetTaskService.searchUsersByCondition(paraMap);
        return userMapList.size();
    }

    /**
     * @author: shenyouyi
     * @date: 2020-08-31
     * @param: entity
     * @return
     * @description: 重写方法，设置记录人相关属性
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity) throws ServiceException {
        super.beforeInsert(entity);
        MeetTaskMasterEntity meetTaskMasterEntity = (MeetTaskMasterEntity)entity;
        String usercode = meetTaskMasterEntity.getUsercode();
        // 设置归属部门信息--办理人所在部门
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(CboUserEntity.FieldCode + " = '{0}'", usercode);
        CboUserEntity cboUserEntity = cboUserService.queryObjectByWhere(sql.toString());
        CboDeptEntity cboDeptEntity = cboDeptService.queryById(String.valueOf(cboUserEntity.getDeptid()));
        meetTaskMasterEntity.setDeptcode(cboDeptEntity.getCode());
        meetTaskMasterEntity.setDeptname(cboDeptEntity.getName());
        // 设置归属公司信息
        CboOrgEntity cboOrgEntity = cboOrgService.queryById(String.valueOf(cboUserEntity.getOrgid()));
        meetTaskMasterEntity.setOrgcode(cboOrgEntity.getCode());
        meetTaskMasterEntity.setOrgname(cboOrgEntity.getName());
    }
}
